package Service.Impl;

import Dao.SignRecordDao;
import Model.SignRecord;
import Service.SignRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.pages.Page;


import java.util.List;

@Service
public class SignRecordServiceImpl implements SignRecordService{

    @Autowired
    private SignRecordDao signRecordDao;

    /**
     * 查询所有签到/签退记录
     * @return
     */
    @Override
    public List<SignRecord> findSignRecord() {
        return signRecordDao.findSignRecord();
    }

    @Override
    public Page<SignRecord> getSignRecordbyName(Integer vId,Integer vHG,Integer vLR,Integer page, Integer rows) {
        //创建用户对象
        int pcounts;
        SignRecord record = new SignRecord();
        //当前页
        record.setStart((page-1)*rows);
        //每页数目
        record.setRows(rows);
        //查询用户列表总记录数
        Integer count = signRecordDao.getSignRecordCount(vHG,vLR);
        List<SignRecord>  pictures = signRecordDao.getSignRecordbyName(vId,vHG,vLR,((page-1)*rows),rows);
        //计算总页数
        if((count <= rows) && (count !=0 )){
            pcounts = 1;
        }else if((count%rows) == 0) {
            pcounts = count / rows;
        }else{
            pcounts = (int)Math.ceil((count/rows)+1);
        }
        //创建Page返回对象
        Page<SignRecord> result = new Page<>();
        result.setPage(page);
        result.setRows(pictures);
        result.setSize(rows);
        result.setTotal(count);
        result.setPcounts(pcounts);
        return result;
    }

    @Override
    public Page<SignRecord> getAbnormalbyName(Integer vId,Integer vHG,Integer vLR,Integer page, Integer rows) {
        //创建用户对象
        int pcounts;
        SignRecord record = new SignRecord();
        //当前页
        record.setStart((page-1)*rows);
        //每页数目
        record.setRows(rows);
        //查询用户列表总记录数
        Integer count = signRecordDao.getAbnormalCount(vHG,vLR);
        List<SignRecord>  pictures = signRecordDao.getAbnormalbyName(vId,vHG,vLR,((page-1)*rows),rows);
        //计算总页数
        if((count <= rows) && (count !=0 )){
            pcounts = 1;
        }else if((count%rows) == 0) {
            pcounts = count / rows;
        }else{
            pcounts = (int)Math.ceil((count/rows)+1);
        }
        //创建Page返回对象
        Page<SignRecord> result = new Page<>();
        result.setPage(page);
        result.setRows(pictures);
        result.setSize(rows);
        result.setTotal(count);
        result.setPcounts(pcounts);
        return result;
    }

    @Override
    public int delSignRecord(int id) {
        return signRecordDao.deleteSignRecord(id);
    }

}
